package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.WillingnessSurvey;
import com.xmy.cultivate.entity.WillingnessSurveyOption;
import com.xmy.cultivate.entity.WillingnessSurveyOptionParameter;
import com.xmy.cultivate.service.IWillingnessSurveyOptionParameterService;
import com.xmy.cultivate.service.IWillingnessSurveyOptionService;
import com.xmy.cultivate.service.IWillingnessSurveyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/willingnessSurvey")
@Scope("prototype")
public class WillingnessSurveyController {

    @Autowired
    IWillingnessSurveyService iWillingnessSurveyService;

    @Autowired
    IWillingnessSurveyOptionService iWillingnessSurveyOptionService;

    @Autowired
    IWillingnessSurveyOptionParameterService iWillingnessSurveyOptionParameterService;

    @GetMapping()
    @ApiOperation("验证是否提交过")
    public long index(@RequestParam Map<String,Object> reMap) {
        int subjectsId = 1;
        if (reMap.get("subjectsId") != null && !reMap.get("subjectsId").toString().isEmpty()) {
            subjectsId = Integer.parseInt(reMap.get("subjectsId").toString());
        }
        long count = iWillingnessSurveyService.count(new QueryWrapper<WillingnessSurvey>()
                .eq("student_id", reMap.get("studentId"))
                .eq("year_part",reMap.get("yearPart"))
                .eq("quarter_num",reMap.get("quarterNum"))
                .eq("subjects_id", subjectsId)
                .eq("is_fill",1));
        if (count > 0) {
            count = 1;
        }
        return count;
    }

    @GetMapping("option")
    @ApiOperation("选项列表")
    public List<WillingnessSurveyOption> option() {
        List<WillingnessSurveyOption> list = iWillingnessSurveyOptionService.list();
        for (WillingnessSurveyOption option : list) {
            List<WillingnessSurveyOptionParameter> type = iWillingnessSurveyOptionParameterService.list(new QueryWrapper<WillingnessSurveyOptionParameter>().eq("type", option.getType()));
            option.setParameterList(type);
        }
        return list;
    }
}
